<?php
/**
 * model
 * 模型
 * @author Administrator
 * @version
 */
class Model_Tables_Model extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'model';
    //protected $_modeltype = array('0'=>'直播','1'=>'点播','2'=>'专题','3'=>'文本');
    protected $_modeltype = array('4'=>'主表','5'=>"子表");
    protected $_modelsql = array('0'=>"(                                                   
	                  `contentid` int(11) unsigned NOT NULL COMMENT '编号',                    
	                  `content` mediumtext NOT NULL COMMENT '内容',                                 
	                  `urllow` varchar(200) NOT NULL default '' COMMENT '低码流url',               
	                  `urlmedium` varchar(200) NOT NULL default '' COMMENT '中码流url',            
	                  `urlhigh` varchar(200) NOT NULL default '' COMMENT '高码流url',  
	                  `ists` tinyint(1) NOT NULL default '0' COMMENT '是否时移',                  
	                  `epgurl` varchar(200) NOT NULL default '' COMMENT '采集地址',               
	                  `epgtype` tinyint(1) NOT NULL default '0' COMMENT '采集类型',               
	                  `epgtime` int(11) NOT NULL default '0' COMMENT '已采集时间',               
	                  `xmlpath` varchar(200) NOT NULL default '' COMMENT '节目单xml保存路径', 
	                  `uploader` varchar(200) NOT NULL default '' COMMENT '上传服务器',
	                  `tsjust` tinyint(1) NOT NULL default '0' COMMENT '时间校正',
	                  PRIMARY KEY  (`contentid`)                                                      
	                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ",
				'1'=>"(                                                                 
	                 `contentid` int(11) unsigned NOT NULL COMMENT '编号',                                 
	                 `content` mediumtext NOT NULL COMMENT '内容',                                              
	                 `urllow` varchar(200) NOT NULL default '' COMMENT '低码流url',                            
	                 `urlmedium` varchar(200) NOT NULL default '' COMMENT '中码流url',                         
	                 `urlhigh` varchar(200) NOT NULL default '' COMMENT '高码流url',      
	                 `uuidlow` text NOT NULL default '' COMMENT '低码流uuid',                            
	                 `uuidmedium` text NOT NULL default '' COMMENT '中码流uuid',                         
	                 `uuidhigh` text NOT NULL default '' COMMENT '高码流uuid',                                       
	                 `vodtype` tinyint(1) NOT NULL default '0' COMMENT '点播来源 0上传 1 录制 2 标注',  
	                 `mediatype` tinyint(1) NOT NULL default '0' COMMENT '媒体类型',                          
	                 `subtitlingcn` varchar(255) NOT NULL default '' COMMENT '中文字幕',                      
	                 `subtitlingen` varchar(255) NOT NULL default '' COMMENT '英文字幕',                      
	                 `starttime` date NOT NULL default '2009-01-01' COMMENT '版权开始时间',                       
	                 `endtime` date NOT NULL default '2009-01-01' COMMENT '版权到期时间',  
	                 `hassync` smallint(1) NOT NULL default '0' COMMENT '是否已经获取信息',
	                 `infolow` mediumtext NOT NULL COMMENT '低码流节目信息',      
	                 `infomedium` mediumtext NOT NULL COMMENT '中码流节目信息',      
	                 `infohigh` mediumtext NOT NULL COMMENT '高码流节目信息',                              
	                 PRIMARY KEY  (`contentid`)                                                                   
	               ) ENGINE=InnoDB DEFAULT CHARSET=utf8",
				'2'=>"(                              
	                     `contentid` int(11) unsigned NOT NULL COMMENT '编号',  
	                     `content` mediumtext NOT NULL COMMENT '内容',               
	                     PRIMARY KEY  (`contentid`)                                    
	                   ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ",
				'3'=>"(                                 
	                  `contentid` int(11) unsigned NOT NULL COMMENT '编号',  
	                  `content` mediumtext NOT NULL COMMENT '内容',               
	                  PRIMARY KEY  (`contentid`)                                    
	                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ",
				'4'=>"(                                 
	                  `id` int(11) NOT NULL auto_increment COMMENT '编号',              
	                  PRIMARY KEY  (`id`)                                    
	                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ",
    			'5'=>"(
    		 		  `id` int(11) NOT NULL auto_increment COMMENT '编号', 
	                  `pid` int(11) NOT NULL COMMENT '父编号',
	                  PRIMARY KEY  (`id`)
	                ) ENGINE=InnoDB DEFAULT CHARSET=utf8 "
				);
	protected $_fieldinsertsql = array('0'=>array(
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'contentid', '编号', 'hidden','1', '0','1', '0')",                                                  
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'content', '内容', 'editor','1', '0','2', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'urllow', '低码流url', 'text','1', '0','5', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'urlmedium', '中码流url', 'text','1', '0','4', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'urlhigh', '高码流url', 'text','1', '0','3', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'ists', '是否时移', 'radio','1', '0','9', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'epgurl', '采集地址', 'text','1', '0','11', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled,defaultvalue)values( ?, 'epgtype', '采集类型', 'select','1', '0','12', '0','array(0=>网易节目单,1=>编码器节目单)')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'epgtime', '已采集时间', 'hidden','1', '0','13', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'xmlpath', '节目单xml保存路径', 'text','1', '0','14', '0') ",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled,defaultvalue)values( ?, 'uploader', '上传服务器', 'text','1', '0','15', '0','\$uploader')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'tsjust', '时间校正', 'text','1', '0','16', '0')"
	
	),
				'1'=>array(
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'contentid', '编号', 'hidden','1', '0','1', '0')",                                                  
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'content', '内容', 'editor','1', '0','2', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'urllow', '低码流url', 'text','1', '0','5', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'urlmedium', '中码流url', 'text','1', '0','4', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'urlhigh', '高码流url', 'text','1', '0','3', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'uuidlow', '低码流uuid', 'hidden','1', '0','6', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'uuidmedium', '中码流uuid', 'hidden','1', '0','7', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'uuidhigh', '高码流uuid', 'hidden','1', '0','8', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'vodtype', '点播来源 0上传 2 录制1 标注', 'select','1', '0','9', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'mediatype', '媒体类型', 'select','1', '0','10', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'subtitlingcn', '中文字幕', 'text','1', '0','11', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'subtitlingen', '英文字幕', 'text','1', '0','12', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'starttime', '版权开始时间', 'datetime','1', '0','13', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'endtime', '版权到期时间', 'datetime','1', '0','14', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'hassync', '是否已经获取信息', 'hidden','1', '0','15', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'infolow', '低码流节目信息', 'hidden','1', '0','16', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'infomedium', '中码流节目信息', 'hidden','1', '0','17', '0')", 
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'infohigh', '高码流节目信息', 'hidden','1', '0','18', '0')"
				),
				'2'=>array(
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'contentid', '编号', 'hidden','1', '0','1', '0')",                                                  
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'content', '内容', 'editor','1', '0','2', '0')"),
				'3'=>array(
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'contentid', '编号', 'hidden','1', '0','1', '0')",                                                  
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'content', '内容', 'editor','1', '0','2', '0')"),
				'4'=>array(
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'id', '编号', 'hidden','1', '0','1', '0')"),
				'5'=>array(
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'id', '编号', 'hidden','1', '0','1', '0')",
					"insert into modelfield ( modelid, field, name,formtype, issystem, issearch,	listorder, disabled)values( ?, 'pid', '编号', 'hidden','1', '0','1', '0')"),
						
				);
    
    public function add( $params )
    {
        $db = $this->getAdapter();    
        $v = array(
	        $params['name'],
	        $params['description'],
	        $params['tablename'],
	        0,
	        $params['modeltype'],
	        1
        );
        $createsql = "create table `".$params['tablename']."`".$this->_modelsql[$params['modeltype']];
        $insertsql = $db->quoteInto("insert into model ( name, description, tablename, disabled, modeltype,issystem) values (?)",$v);
        $db->beginTransaction();
			try {
			    $db->query($createsql);
			    $db->query($insertsql);
			    $modelid = $db->lastInsertId();
			    foreach($this->_fieldinsertsql[$params['modeltype']] as $filedsql)
			    {
			    	$db->query($db->quoteInto($filedsql,$modelid));
			    }
			    $db->commit();
			} catch (Exception $e) {
				print_r($e) ;
			    $db->rollBack();
			    return false;
			}
		return true;
    }
    public function modify( $params, $modelid )
    {
        $data = array(
		'name' => $params['name'],
		'description' => $params['description'],
		'tablename' => $params['tablename'],
		'disabled' => $params['disabled'],
		'modeltype' => $params['modeltype'],
		'issystem' => $params['issystem'],
        );
        $where = 'modelid = '.$modelid ;
        return $this->update($data , $where );
    }
    public function modifyAjax( $modelid, $field, $value,$oldvalue )
    {
    	if($field == "tablename")//修改表名
    	{
    		$db = $this->getAdapter();
    		$db->beginTransaction();
			try {
			    $db->query("rename table `".$oldvalue."` to `".$value."`");
			    $db->query("update `model` set `tablename` = '".$value."' where modelid = '".$modelid."'");
			    $db->commit();
			} catch (Exception $e) {
			    $db->rollBack();
			    return false;
			}
			return true;
    	}
    	else
    	{
    		$data = array($field => $value);
        	$where = 'modelid = '.$modelid;
        	return $this->update($data , $where );
    	}
    	
    }
  	public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update model set  $field =not  $field  where modelid=' $pri'";
		return $db->query($sql);
    }
    public function del( $modelid ,$tablename)
    {
    	$db = $this->getAdapter();
    	$db->beginTransaction();
		try {
		    $db->query("drop table `".$tablename."`");
		    $db->query("delete from `model` where modelid = '".$modelid."'");
		    $db->query("delete from `modelfield` where modelid = '".$modelid."'");
		    $db->commit();
		} catch (Exception $e) {
		    $db->rollBack();
		    return false;
		}
		return true;
    }
    public function getInfo($modelid)
    {
        $row = $this->fetchRow('modelid = ' . $modelid);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    public function getCount($s="")
    {
    	$select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
    		$db = $this->getAdapter();
    		$where = $db->quoteInto("name like ?", "%$s%");
    		$select = $select->where($where);
    	}
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    public function getList($page, $numPerPage = 10,$s="")
    {
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('modelid');        
		if($s!="")     
		{
			$db = $this->getAdapter();
			$where = $db->quoteInto("name like ?", "%$s%");
		}
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    /*
     * 获取模型的类型
     */
    public function getModelType()
    {
    	return $this->_modeltype;
    }
}
 
 